package com.bird.ecmainmenu3.presenter;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.bird.ecmainmenu3.base.OnAppStateListener;
import com.bird.ecmainmenu3.base.OnLoadAppsListener;
import com.bird.ecmainmenu3.model.AppInfo;
import com.bird.ecmainmenu3.model.LoadAppsTask;
import com.bird.ecmainmenu3.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppsManager implements OnLoadAppsListener {
    private static final String TAG = Utils.TAG + AppsManager.class.getSimpleName();

    private final List<AppInfo> mAppList = Collections.synchronizedList(new ArrayList<>());

    private static final AppsManager instance = new AppsManager();

    private boolean isAppListShowing = false;

    private LoadAppsTask mLoadAppsTask;

    private OnAppStateListener mOnAppStateListener;
    public void setOnAppStateListener(OnAppStateListener listener) {
        mOnAppStateListener = listener;
    }

    public static AppsManager getInstance() {
        return instance;
    }

    private AppsManager() {

    }

    public void startApp(@NonNull Context context, String pkg, String cls) {
        Log.d(TAG, "startApp: pkg=" + pkg + ", cls=" + cls);
        Utils.startApp(context, pkg, cls);
    }

    public void notifyClose() {
        if (mOnAppStateListener != null) {
            Log.d(TAG, "onNotifyClose: ");
            mOnAppStateListener.onNotifyClose();
        }
    }

    private void notifyOpen() {
        if (mOnAppStateListener != null) {
            Log.d(TAG, "onNotifyOpen: ");
            mOnAppStateListener.onNotifyOpen();
        }
    }

    private void addAppInfoToList(@NonNull AppInfo appInfo) {
        String pkg = appInfo.getPackageName();
        Log.d(TAG, "addAppInfo: pkg=" + pkg);

        for (AppInfo app : mAppList) {
            if (app.getPackageName().equals(pkg)) {
                Log.d(TAG, "app is already exist");
                return;
            }
        }

        mAppList.add(appInfo);
    }

    private void removeAppInfoFromList(String packageName) {
        Log.d(TAG, "removeAppInfoFromList: pkg=" + packageName);
        mAppList.removeIf(app -> app.getPackageName().equals(packageName));
    }

    public void notifyAppAdded(@NonNull AppInfo appInfo) {
        String packageName = appInfo.getPackageName();
        Log.d(TAG, "notifyAppAdded: " + packageName);
        addAppInfoToList(appInfo);

        if (mOnAppStateListener != null) {
            mOnAppStateListener.onAppAdded(appInfo);
        }
    }

    public void notifyAppRemoved(String packageName) {
        Log.d(TAG, "notifyAppRemoved: " + packageName);
        if (mOnAppStateListener != null) {
            for (AppInfo app : mAppList) {
                if (app.getPackageName().equals(packageName)) {
                    mOnAppStateListener.onAppRemoved(app);
                    break;
                }
            }
        }
        removeAppInfoFromList(packageName);
    }

    private void notifyAppUpdated() {
        if (mOnAppStateListener != null) {
            mOnAppStateListener.onUpdateApps(mAppList);
        }
    }

    public boolean isAppListShowing() {
        return isAppListShowing;
    }

    public void setAppListShowing(boolean isShowing) {
        this.isAppListShowing = isShowing;
        if (isShowing) {
            notifyOpen();
        } else {
            notifyClose();
        }
    }

    public void startLoadAppTask(@NonNull Context context) {
        mLoadAppsTask = new LoadAppsTask(context, this);
        mLoadAppsTask.execute();
    }

    public void stopLoadAppTask() {
        if (mLoadAppsTask != null) {
            mLoadAppsTask.cancel(true);
            mLoadAppsTask = null;
        }
    }

    @Override
    public void onLoadFinished(List<AppInfo> apps) {
        int size = apps.size();
        Log.d(TAG, "onLoadFinished: " + size);

        mAppList.clear();
        mAppList.addAll(apps);
    }

    public List<AppInfo> getAppList() {
        return mAppList;
    }
}
